package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.UmsCommunityMapper;
import com.macro.mall.model.UmsCommunity;
import com.macro.mall.portal.service.UmsCommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UmsCommunity的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class UmsCommunityServiceImpl implements UmsCommunityService {

    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

    @Autowired
    private UmsCommunityMapper umsCommunityMapper;

    @Override
    public List<UmsCommunity> find(UmsCommunity request) {
        //TODO这里可以做通过Assist做添加查询

        Assist assist = new Assist();
        if (StrUtil.isNotEmpty(request.getCommunityName())) {
            assist.andLike("community_name", "%" + request.getCommunityName() + "%");
        }
        if (StrUtil.isNotEmpty(request.getProvince())) {
            JSONObject jsonObject = JSONUtil.parseObj(request.getProvince());
            assist.andLike("province", "%" + jsonObject.get("provCode") + "%");
        }
        if (StrUtil.isNotEmpty(request.getCityCode())) {
            JSONObject jsonObject = JSONUtil.parseObj(request.getCityCode());
            assist.andLike("city_code", "%" + jsonObject.get("cityCode") + "%");
        }
        if (StrUtil.isNotEmpty(request.getCountyCode())) {
            JSONObject jsonObject = JSONUtil.parseObj(request.getCountyCode());
            assist.andLike("county_code", "%" + jsonObject.get("countyCode") + "%");
        }
        List<UmsCommunity> result = umsCommunityMapper.selectUmsCommunity(assist);
        if (isDebugEnabled) {
            log.debug("执行获取UmsCommunity数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public UmsCommunity findOne(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsCommunity的id获得UmsCommunity对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过UmsCommunity的id获得UmsCommunity对象-->失败:id不能为空");
        }
        UmsCommunity result = umsCommunityMapper.selectUmsCommunityById(id);
        if (isDebugEnabled) {
            log.debug("执行通过UmsCommunity的id获得UmsCommunity对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(UmsCommunity value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将UmsCommunity中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = umsCommunityMapper.insertNotNullUmsCommunity(value);
        if (isDebugEnabled) {
            log.debug("执行将UmsCommunity中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(UmsCommunity value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsCommunity的id更新UmsCommunity中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过UmsCommunity的id更新UmsCommunity中属性不为null的数据-->失败:对象为null");
        }
        int result = umsCommunityMapper.updateNotNullUmsCommunityById(value);
        if (isDebugEnabled) {
            log.debug("执行通过UmsCommunity的id更新UmsCommunity中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsCommunity的id删除UmsCommunity-->失败:id不能为空");
            }
            Asserts.fail("执行通过UmsCommunity的id删除UmsCommunity-->失败:id不能为空");
        }
        int result = umsCommunityMapper.deleteUmsCommunityById(id);
        if (isDebugEnabled) {
            log.debug("执行通过UmsCommunity的id删除UmsCommunity-->结果:", result);
        }
        return CommonResult.success(null);
    }


}